from Selenium2Library.keywords.keywordgroup import KeywordGroup
from Selenium2Library.locators import ElementFinder

class _BreezeMailKeywords(KeywordGroup):
    """Library for Breeze Mail Keywords purposes.
    
    This library is used in Breeze projects.
    """
    def __init__(self):
        """Does nothing. init only"""
        print '_BreezeMailKeywords.__init__'
                    
    def read_bmw_mail_list(self):
        """Used in mail.tsv it is used for mail comparing between BMW and iBanking.
        
        Comparing the mail count, unread mail and mail list.
        
        This keyword is used for read the mail list in BMW.
        
        See `Read Ibnk Mail List`
        """
        return self._run('read_bmw_mail_list')

    def _read_bmw_mail_list_pk(self):        
        rows4 = self._element_find("//dt[@id]", False, False)
        self._info(len(rows4))
        
        date = []
        for i in range(0, len(rows4)):
            date.append(self.get_element_attr(rows4[i], "id"))   
        self._info(date)
            
        j = len(rows4)
        #find the number of sibling children by reversed order    
        countdate = []
        for i in range(0, len(rows4)):
            rows3 = self._element_find("//dt[@id][" + str(j) + "]/following-sibling::dd/a/span", False, False)
            j = j - 1
            self._info(len(rows3))
            countdate.append(len(rows3))  
        self._info(countdate)
        
        #the no of records for each day
        countdate1 = []
        j = len(rows4)
        for i in range(0, len(rows4)):
            j = j - 1
            if j == 0:
                countdate1.append(countdate[j])
            else:
                countdate1.append(countdate[j] - countdate[j - 1])
        self._info(countdate1)
        
        #the begin record for each day
        countdate2 = []
        for i in range(0, len(rows4)):
            if i == 0:
                countdate2.append(countdate1[i])
            else:
                countdate2.append(countdate2[i - 1] + countdate1[i])
        self._info(countdate2)

        maillist = []
        rows3 = self._element_find("//dd[@id]", False, False)
        self._info(len(rows3))
        
        date1 = []
        j = 0
        k = 0        
        for i in range(0, len(rows3)): #TODO
            sub = rows3[i].find_elements_by_xpath("./a/span")
            read = rows3[i].find_elements_by_xpath("./a")
            
            k = k + 1
            if k <= countdate2[j]:                    
                date1 = date[j]
                self._info(date1)
            else:
                j = j + 1
                date1 = date[j]
                self._info(date1)
            
            #2012-10-02 target format is 01/10/2011
            year = date1.split("-")[0]
            month = date1.split("-")[1]
            day = date1.split("-")[2]
            self._info('%s> %s> %s>' % (sub[0].text, day + "/" + month + "/" + year, read[0].text))
            maillist.append([sub[0].text,
                            day + "/" + month + "/" + year,
                            self.get_element_attr(read[0], "class")])
        self._info(len(maillist))
        maillist.sort()
        return maillist

    def _read_bmw_mail_list_id(self):        
        rows4 = self._element_find("//dt[@id]", False, False)
        self._info(len(rows4))
        
        date = []
        for i in range(0, len(rows4)):
            date.append(self.get_element_attr(rows4[i], "id"))   
        self._info(date)
            
        j = len(rows4)
        #find the number of sibling children by reversed order    
        countdate = []
        for i in range(0, len(rows4)):
            rows3 = self._element_find("//dt[@id][" + str(j) + "]/following-sibling::dd/a/span", False, False)
            j = j - 1
            self._info(len(rows3))
            countdate.append(len(rows3))  
        self._info(countdate)
        
        #the no of records for each day
        countdate1 = []
        j = len(rows4)
        for i in range(0, len(rows4)):
            j = j - 1
            if j == 0:
                countdate1.append(countdate[j])
            else:
                countdate1.append(countdate[j] - countdate[j - 1])
        self._info(countdate1)
        
        #the begin record for each day
        countdate2 = []
        for i in range(0, len(rows4)):
            if i == 0:
                countdate2.append(countdate1[i])
            else:
                countdate2.append(countdate2[i - 1] + countdate1[i])
        self._info(countdate2)

        maillist = []
        rows3 = self._element_find("//dd[@id]", False, False)
        self._info(len(rows3))
        
        date1 = []
        j = 0
        k = 0        
        for i in range(0, len(rows3)): #TODO
            sub = rows3[i].find_elements_by_xpath("./a/span")
            read = rows3[i].find_elements_by_xpath("./a")
            
            k = k + 1
            if k <= countdate2[j]:                    
                date1 = date[j]
                self._info(date1)
            else:
                j = j + 1
                date1 = date[j]
                self._info(date1)
            
            #2012-10-02 target format is 01/10/2011
            year = date1.split("-")[0]
            month = date1.split("-")[1]
            day = date1.split("-")[2]
            self._info('%s> %s> %s>' % (sub[0].text, day + "/" + month + "/" + year, read[0].text))
            maillist.append([sub[0].text,
                            day + "/" + month + "/" + year,
                            self.get_element_attr(read[0], "class")])
        self._info(len(maillist))
        maillist.sort()
        return maillist
    
    def read_ibnk_mail_list(self):
        """Used in mail.tsv it is used for mail comparing between BMW and iBanking.
        
        Comparing the mail count, unread mail and mail list.
        
        This keyword is used for read the mail list in iBanking.
        
        See `Read BMW Mail List`
        """
        return self._run('read_ibnk_mail_list')

    def _read_ibnk_mail_list_pk(self):
        rows = self._element_find("//*[@id='mailForm']/table/tbody/tr[3]/td/table[3]/tbody/tr/td", False, False)
        self._info(len(rows))
        
#        Total: 35,1 of 2 page(s).
        mailpage = int((rows[0].text.split(",")[1]).split(" ")[2])
        self._info(mailpage)
        
        maillist1 = []
        
        rows3 = self._element_find("//*[@id='vMailList']/tbody/tr", False, False)
        self._info(len(rows3))
        
        temp = self._read_ibnk_mail_list_pk_single_page(rows3)
        maillist1 = maillist1 + temp

        if mailpage > 1:
            for i in range(1, mailpage):
                if i == 1:
                    element = self._element_find("//*[@id='ignorePrint_paging']/tbody/tr/td/a[" + str(i) + "]", True, False)
                elif i < 4:
                    element = self._element_find("//*[@id='ignorePrint_paging']/tbody/tr/td/a[" + str(i + 2) + "]", True, False)
                else:
                    element = self._element_find("//*[@id='ignorePrint_paging']/tbody/tr/td/a[6]", True, False)
                element.click()
                self.wait_until_page_contains("Mailbox")
                
                rows3 = self._element_find("//*[@id='vMailList']/tbody/tr", False, False)
                self._info(len(rows3))
                
                temp = self._read_ibnk_mail_list_pk_single_page(rows3)
                maillist1 = maillist1 + temp
            
            maillist1.sort()
            return maillist1
        else:
            maillist1.sort()
            return maillist1

    def _read_ibnk_mail_list_pk_single_page(self, rows3):
        maillist = []
        for i in range(0, len(rows3)): #TODO
            sub = rows3[i].find_elements_by_xpath("./td[4]")
            date = rows3[i].find_elements_by_xpath("./td[2]")
            read = rows3[i].find_elements_by_xpath("./td[3]")
#            unread = rows3[i].find_elements_by_xpath("./td[3]/b")
#                currency = rows3[i].find_elements_by_xpath("./td[2]")
            self._info('%s> %s>' % (sub[0].text, date[0].text))
            
            #array from 0;elements from 1
            if (self.get_matching_xpath_count("//*[@id='vMailList']/tbody/tr" + "[" + str(i + 1) + "]" + "/td[3]/b")) == str(1):
                maillist.append([sub[0].text,
                            date[0].text,
                            "message  unread"])                
            else:
                maillist.append([sub[0].text,
                            date[0].text,
                            "message"])

        return maillist
    
    def _read_ibnk_mail_list_id(self):
        '''To be implemented
        '''
        return true
    
    def read_ibnk_mail_count(self):
        """Used in mail.tsv it is used for mail comparing between BMW and iBanking.
        
        Comparing the mail count, unread mail and mail list.
        
        This keyword is used for read the mail count in iBanking.
        """
        return self._run('read_ibnk_mail_count')

    def _read_ibnk_mail_count_pk(self):
        element = self._element_find("//*[@id='ignorePrint_tbl_content_top']/div/table[3]/tbody/tr[3]/td[2]/a", True, False)
        element.click()
        self.wait_until_page_contains("Mailbox")
        
        rows3 = self._element_find("//*[@id='mailForm']/table/tbody/tr[3]/td/table[3]/tbody/tr/td", False, False)
        self._info(len(rows3))
        
        mailcount = ""
#        Total: 35,1 of 2 page(s).
        mailcount = (rows3[0].text.split(",")[0]).split(" ")[1]
        
        return mailcount

    def _read_ibnk_mail_count_id(self):
        '''To be implemented
        '''
        return true
        
    def read_ibnk_mail_unread_count(self):
        """Used in mail.tsv it is used for mail comparing between BMW and iBanking.
        
        Comparing the mail count, unread mail and mail list.
        
        This keyword is used for read the unread mail count in iBanking.
        """
        return self._run('read_ibnk_mail_unread_count')

    def _read_ibnk_mail_unread_count_pk(self):
  
        rows3 = self._element_find("//*[@id='ignorePrint_tbl_content_top']/div/table[3]/tbody/tr[2]/td[2]/span/span", False, False)
        self._info(len(rows3))
        
        self._info('%s> ' % (rows3[0].text.split(" ")[0]))
        return (rows3[0].text.split(" ")[0])

    def _read_ibnk_mail_unread_count_id(self):
        '''To be implemented
        '''
        return true


